package com.ocm.core.dao.jpa;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.EntityManagerFactory;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.ocm.core.dao.PropertyDefDao;
import com.ocm.core.domain.PropertyDefinition;

@Repository("propertyDefDao")
public class JpaPropertyDefDao extends JpaGenericDao<PropertyDefinition> implements PropertyDefDao {

   @Autowired
   public JpaPropertyDefDao(@Qualifier("ocmEntityManagerFactory") EntityManagerFactory entityManagerFactory) {
      super(PropertyDefinition.class);
      super.setEntityManagerFactory(entityManagerFactory);
   }

   @SuppressWarnings("unchecked")
   public List<PropertyDefinition> findAll() {
      List<PropertyDefinition> properties = this.getJpaTemplate().findByNamedQuery(PropertyDefinition.FIND_ALL);
      return properties;
   }

   @SuppressWarnings("unchecked")
   public PropertyDefinition findName(String name, Long storageId) {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(PropertyDefinition.NAME_PARA, name);
      paras.put(PropertyDefinition.ID_PARA, storageId);
      List<PropertyDefinition> properties = this.getJpaTemplate().findByNamedQueryAndNamedParams(PropertyDefinition.FIND_NAME, paras);
      if (properties.size() > 0){
         return properties.get(0);
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   public List<PropertyDefinition> findIds(Long[] ids) {
      if (ids.length > 0){
         List<Long> listId = Arrays.asList(ids);
         Map<String, Object> paras = new HashMap<String, Object>();
         paras.put(PropertyDefinition.LIST_ID_PARA, listId);
         List<PropertyDefinition> properties = this.getJpaTemplate().findByNamedQueryAndNamedParams(PropertyDefinition.FIND_ID_LIST, paras);
         if (properties.size() > 0){
            return properties;
         }
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   public List<PropertyDefinition> findNames(String[] names, Long storageId) {
      if (names.length > 0){
         List<String> listName = Arrays.asList(names);
         Map<String, Object> paras = new HashMap<String, Object>();
         paras.put(PropertyDefinition.LIST_NAME_PARA, listName);
         paras.put(PropertyDefinition.ID_PARA, storageId);
         List<PropertyDefinition> properties = this.getJpaTemplate().findByNamedQueryAndNamedParams(PropertyDefinition.FIND_NAME_LIST, paras);
         if (properties.size() > 0){
            return properties;
         }
      }
      return null;
   }

   @SuppressWarnings("unchecked")
   public List<PropertyDefinition> findAllFromStorageId(Long storageId) {
      Map<String, Object> paras = new HashMap<String, Object>();
      paras.put(PropertyDefinition.ID_PARA, storageId);
      List<PropertyDefinition> properties = this.getJpaTemplate().findByNamedQueryAndNamedParams(PropertyDefinition.FIND_STORAGEID, paras);
      if (properties.size() > 0){
         return properties;
      }
      return null;
   }
}
